import { GlobalService } from '@services/global.service';
import { Component, OnInit, Input } from '@angular/core';
import { NavController, ModalController, Events } from '@ionic/angular';
import { VoiceModal } from '@modals/select-voice/select-voice';
// import { EmitService } from '@providers/emit.service';
import { CacheService } from '@services/cache.service';
import { JPush } from '@jiguang-ionic/jpush/ngx';
import { UpdateService } from '@services/update.service';
import { AlarmAudioService } from '@services/alarmAudio.service';


@Component({
  selector: 'settings',
  templateUrl: 'settings.html',
  styleUrls: ['settings.scss']
})
export class SetPage implements OnInit {
  pushstate;     // 告警推送设置
  voiceState;   // 语音告警设置

  // private test;
  @Input() selelctName;
  @Input() selelcedtName;
  @Input() state: boolean;

  voiceType;

  constructor(
    public navCtrl: NavController,
    public modalCtrl: ModalController,
    // public emitEvent: EmitService,
    public global: GlobalService,
    // public app: App,
    public events: Events,
    public jpush: JPush,
    public updateSerice: UpdateService,
    private cache: CacheService,
    private audio: AlarmAudioService
  ) {
    events.subscribe('voiceType', (voice) => {
      console.log(voice);
      this.cache.defaultVoice = {
        voiceType: this.state,
        voiceName: voice.voiceName,
        voicePath: voice.voicePath,
      };
      this.voiceType = this.cache.defaultVoice.voiceName;
    });
    if (this.cache.jpushstate === '1') {
      this.pushstate = true;
    } else {
      this.pushstate = false;
    }

    if (this.cache.voiceState === 'on') {
      this.voiceState = true;
    } else {
      this.voiceState = false;
    }
    this.state = this.cache.defaultVoice && this.cache.defaultVoice.voiceType ? this.cache.defaultVoice.voiceType : false;
    this.voiceType = this.cache.defaultVoice && this.cache.defaultVoice.voiceName ? this.cache.defaultVoice.voiceName : '默认';
  }

  ngOnInit() {
  }

  voiceToggle() {
    this.cache.defaultVoice = {
      voiceType: this.state,
      voiceName: this.state && this.cache.defaultVoice && this.cache.defaultVoice.voiceName ? this.cache.defaultVoice.voiceName : '默认',
      voicePath: this.state && this.cache.defaultVoice && this.cache.defaultVoice.voicePath ? this.cache.defaultVoice.voicePath : 'assets/mp3/0.mp3',
    };
  }

  togglePushNotification() {
    if (this.pushstate) {
      this.jpush.setAlias({
        alias: this.cache.userinfo.userId.toString(),
        sequence: new Date().getTime()
      }).then((args) => {
        console.log('jpush setAlias succeed:', args);
      }).catch(err => {
        console.log('jpush setAlias error:', err);
      });
      this.cache.jpushstate = '1';
    } else {
      this.jpush.deleteAlias({
        alias: '',
        sequence: new Date().getTime()
      }).then((args) => {
        console.log('jpush deleteAlias succeed:', args);
      }).catch(err => {
        console.log('jpush deleteAlias error:', err);
      });
      this.cache.jpushstate = '0';
    }
  }

  toggleVoiceAlarm() {
    if (this.voiceState) {
      this.audio.beginPlay();
      this.cache.voiceState = 'on';
    } else {
      this.audio.stopPlay();
      this.cache.voiceState = 'off';
    }
  }

  async toVoice() {
    this.selelcedtName = this.voiceType || '默认';
    const data: Object = {
      musicType: this.selelcedtName,
    };
    const adModal = await this.modalCtrl.create({
      component: VoiceModal,
      componentProps: { config: data }
    });
    await adModal.present();
  }

  checkUpdate() {
    this.updateSerice.checkAppUpdate(true);
  }

  logout() {
    this.jpush.deleteAlias({
      alias: '',
      sequence: new Date().getTime()
    });
    this.global.isLogin = false;
    this.cache.clearCache();
    this.navCtrl.navigateRoot('login');
  }
}
